﻿using UnityEngine;

public class StateBase
{
    public string stateName { get; private set; }
    
    public string animatorParameter { get; private set; }//动画bool参数
    
    protected ControllerBase controller;
    
    protected  StateMachineBase stateMachine;
    
    protected float stateDuration;
    
    protected bool triggerFinish;//动画结束触发
    
    protected bool triggerCancel;//动画打断播放
    
    public StateBase(ControllerBase controller, StateMachineBase stateMachine,
        string stateName,string animatorParameter = null)
    {
        this.stateName = stateName;
        this.controller = controller;
        this.stateMachine = stateMachine;
        this.animatorParameter = animatorParameter;
    }
    

    public virtual void Enter()
    {
       //Debug.Log($"{controller.gameObject.name}<color=green>{stateName}</color>进入{stateDuration}");
       stateDuration = 0;
       triggerFinish = false;
       triggerCancel = false;
       if (animatorParameter != null)
       {
           controller.mAnimator.SetBool(animatorParameter, true);
       }
    }
    
    public virtual void Enter(object param)
    {
       Enter();
    }

    public virtual void Exit()
    {
       //Debug.Log($"{controller.gameObject.name}<color=red>{stateName}</color>持续{stateDuration}Exit");
       if (animatorParameter!=null)
       {
           controller.mAnimator.SetBool(animatorParameter, false);
       }
    }

    public virtual void Update()
    {
        //Debug.Log($"<color=blue>{stateName}</color>持续{stateDuration}Update");
        stateDuration+=Time.deltaTime;
        //CheckCanUpdate();

    }

    public virtual bool CheckCanUpdate()
    {
        if (controller.IsFrozen())
        {
            controller.SetVelocity(new Vector2(0,controller.GetVelocity().y));
            return false;
        }
        return true;
    }

    public virtual void TriggerFinish()
    {
        triggerFinish = true;
    }
    
    public virtual void TriggerCancel()
    {
        triggerCancel = true;
    }

    public virtual void OnDestroy()
    {
        
    }
}